package com.lmuse.xmr.foodspotting.ui.search;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.dexafree.materialList.card.Card;
import com.dexafree.materialList.card.CardProvider;
import com.dexafree.materialList.listeners.OnDismissCallback;
import com.dexafree.materialList.listeners.RecyclerItemClickListener;
import com.dexafree.materialList.view.MaterialListView;
import com.lmuse.xmr.foodspotting.R;
import com.lmuse.xmr.foodspotting.db.Contract;
import com.lmuse.xmr.foodspotting.db.RecipePresenterImpl;
import com.lmuse.xmr.foodspotting.model.Recipe;
import com.lmuse.xmr.foodspotting.ui.recipe.RecipeActivity;
import com.lmuse.xmr.foodspotting.util.Statistics;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.datatype.BmobFile;

public class SearchResultActivity extends AppCompatActivity implements Contract.RecipeView{

    private EditText searchEt;
    private MaterialListView cardList;

    private Contract.RecipePresenter recipePresenter;

    private List<Recipe> recipeList;
    private String searchTitle;
    private List<String> resultList; //搜索结果的list集合

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_result);
        searchTitle = getIntent().getStringExtra(com.lmuse.xmr.foodspotting.constants.Recipe.SEARCH_TITLE);
        recipePresenter = new RecipePresenterImpl(this);
        initView();
//        recipePresenter.queryRecipe("title", searchTitle, 500);
        recipePresenter.queryByKey("title");
    }

    void initView() {
        searchEt = (EditText) findViewById(R.id.et_search_result);
        searchEt.clearFocus();
        searchEt.setOnFocusChangeListener(new FocusChangeListener());

        cardList = (MaterialListView) findViewById(R.id.cardlist_search_result);
        cardList.addOnItemTouchListener(new CardClickListener());
        cardList.setOnDismissCallback(new CardDismissCallBack());

        recipeList = new ArrayList<>();
        resultList = new ArrayList<>();
    }

    class FocusChangeListener implements View.OnFocusChangeListener {
        @Override
        public void onFocusChange(View view, boolean b) {
            if (b) { //获得焦点
                searchEt.clearFocus();
                SearchResultActivity.this.finish();
            }else { //失去焦点

            }
        }
    }

    class CardClickListener implements RecyclerItemClickListener.OnItemClickListener {

        @Override
        public void onItemClick(@NonNull Card card, int position) {
            Intent startIntent = new Intent(SearchResultActivity.this,
                    RecipeActivity.class);
            startIntent.putExtra(com.lmuse.xmr.foodspotting.constants.Recipe.ID,
                    recipeList.get(position).getObjectId());
            startActivity(startIntent);
        }

        @Override
        public void onItemLongClick(@NonNull Card card, int position) {

        }
    }

    class CardDismissCallBack implements OnDismissCallback {
        @Override
        public void onDismiss(@NonNull Card card, int position) {
            /*int count = (page + 1) * limit - 2;
            if (position == count) {
                Toast.makeText(getActivity(), "当前第" + count + "个", Toast.LENGTH_SHORT).show();
                page++;
                recipePresenter.queryRecipe("label", labelTitle.getText().toString(), limit, page);
            }*/
        }
    }

    @Override
    public void onUploadingImg(Integer value) {

    }

    @Override
    public void onUploadingImg(int curIndex, int curPercent, int total, int totalPercent) {

    }

    @Override
    public void onUploadImgSuccess(BmobFile cover) {

    }

    @Override
    public void onUploadImgSuccess(List<String> urls) {

    }

    @Override
    public void onUploadImgFailed(String msg) {

    }

    @Override
    public void onAddRecipeSuccess() {

    }

    @Override
    public void onAddRecipeFailed(String msg) {

    }

    @Override
    public void onQueryRecipeSuccess(Recipe recipe) {

    }

    @Override
    public void onQueryRecipeSuccess(List<Recipe> recipeList) {
        //        Toast.makeText(getActivity(), "请求完成", Toast.LENGTH_SHORT).show();
        for (Recipe recipe : recipeList) {
            this.recipeList.add(recipe);
            Card card = new Card.Builder(this)
                    .setTag("BIG_IMAGE_CARD")
                    .withProvider(new CardProvider())
                    .setLayout(R.layout.material_big_image_card_layout)
                    .setTitle(recipe.getTitle())
                    .setTitleColor(getResources().getColor(R.color.colorWhite))
                    .setDrawable(recipe.getCoverUrl())
                    .endConfig()
                    .build();
            cardList.getAdapter().add(card);
        }
        cardList.smoothScrollToPosition(0);
    }

    @Override
    public void onQueryRecipeFailed(String msg) {

    }

    @Override
    public void onQueryKeySuccess(List<Recipe> recipeKeyList) {
        List<String> titleList = new ArrayList<>();
        for (Recipe recipe : recipeKeyList) {
            titleList.add(recipe.getTitle());
        }
        resultList = Statistics.search(searchTitle, titleList);
        for (String title : resultList) {
            recipePresenter.queryRecipe("title", title, 500);
        }
    }

    @Override
    public void onQueryKeyFailed(String msg) {
        Toast.makeText(this, "请求食谱列表失败！(" + msg + ")", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDeleteRecipeSuccess() {

    }

    @Override
    public void onDeleteRecipeFailed(String msg) {

    }
}
